package ipebg.mzk.provider.listener;

import ipebg.mzk.facade.entity.LogMongo;
import ipebg.mzk.facade.enums.LogLevel;
import ipebg.mzk.facade.system.service.LogService;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConsumerMessageListener implements MessageListener {
	private static Log log = LogFactory.getLog(ConsumerMessageListener.class);

	@Resource(name = "logServiceImpl")
	LogService logServiceImpl;

	@Override
	public void onMessage(Message message) {
		TextMessage textMsg = (TextMessage) message;
		String strMsg = null, logMsg;
		try {
			strMsg = textMsg.getText();
		} catch (JMSException e1) {
			e1.printStackTrace();
		}
		logMsg = "接收到一个纯文本消息。消息内容是：" + strMsg;
		System.out.println(logMsg);
		log.info(logMsg);

		try {
			logServiceImpl.save(new LogMongo(LogLevel.DEBUG, logMsg));
			Thread.sleep(1000l);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
